home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / euphor14.zip / REFMAN.DOC < prev    next >
Text File  |  1996-05-31  |  70KB  |  1,658 lines

  1.  
  2.         Euphoria Programming Language 
  3.             version 1.4
  4.               Reference Manual
  5.  
  6.  
  7.         (c) 1996 Rapid Deployment Software
  8.     
  9.         Permission is freely granted to anyone
  10.         to copy this manual.
  11.  
  12.  
  13.  
  14.  
  15.             TABLE OF CONTENTS
  16.             =================
  17.  
  18.      Part I - Core Language - refman.doc
  19.  
  20.     1. Introduction 
  21.       
  22.        1.1  Example Program                  
  23.        1.2  Installation             
  24.        1.3  Running a Program    
  25.        1.4  Editing a Program   
  26.        1.5  Distributing a Program
  27.     
  28.     2. Language Definition        
  29.      
  30.        2.1  Objects                 
  31.        2.2  Expressions     
  32.        2.3  Euphoria versus Conventional Languages
  33.        2.4  Declarations 
  34.        2.5  Statements      
  35.        2.6  Top-Level Commands 
  36.     
  37.     3. Debugging 
  38.  
  39.  
  40.      Part II - Library Routines - see library.doc
  41.     
  42.     1. Introduction
  43.             
  44.     2. Routines by Application Area
  45.     
  46.     3. Alphabetical Listing of all Routines
  47.       
  48.  
  49.  
  50.  
  51.     
  52.             1. Introduction
  53.             ===============
  54.         
  55.  Euphoria is a new programming language with the following advantages over 
  56.  conventional languages:
  57.  
  58.  o      a remarkably simple, flexible, powerful language
  59.     definition that is easy to learn and use.  
  60.  
  61.  o      dynamic storage allocation. Variables grow or shrink
  62.     without the programmer having to worry about allocating
  63.     and freeing chunks of memory.  Objects of any size can be 
  64.     assigned to an element of a Euphoria sequence (array).
  65.  
  66.  o      a high-performance, state-of-the-art interpreter that is
  67.     10 to 20 times faster than conventional interpreters such as
  68.     Microsoft QBasic.  
  69.  
  70.  o      lightning-fast pre-compilation. Your program is checked
  71.     for syntax and converted into an efficient internal form at
  72.     over 12,000 lines per second on a 486-50. 
  73.  
  74.  o      extensive run-time checking for: out-of-bounds subscripts,
  75.     uninitialized variables, bad parameter values for library
  76.     routines, illegal value assigned to a variable and many 
  77.     more.  There are no mysterious machine exceptions -- you 
  78.     will always get a full English description of any problem 
  79.     that occurs with your program at run-time, along with a 
  80.     call-stack trace-back and a dump of all of your variable
  81.     values.  Programs can be debugged quickly, easily and
  82.     more thoroughly.
  83.  
  84.  o      features of the underlying hardware are completely hidden. 
  85.     Programs are not aware of word-lengths,    underlying bit-level 
  86.     representation of values, byte-order etc.  
  87.  
  88.  o      a full-screen source debugger and an execution profiler
  89.     are included, along with a full-screen, multi-file editor. 
  90.     On a color monitor, the editor displays Euphoria programs in 
  91.     multiple colors, to highlight comments, reserved words, 
  92.     built-in functions, strings, and level of nesting of brackets. 
  93.     It optionally performs auto-completion of statements, 
  94.     saving you typing effort and reducing syntax errors. This 
  95.     editor is written in Euphoria, and the source code is 
  96.     provided to you without restrictions. You are free to
  97.     modify it, add features, and redistribute it as you wish. 
  98.  
  99.  o      Euphoria programs run under MS-DOS (or Windows or OS/2), but 
  100.      are not    subject to any 64K or 640K memory limitations. You can
  101.     create programs that use the full multi-megabyte memory
  102.     of your computer. A swap file is automatically used when a
  103.     program needs more memory than exists on your machine. 
  104.  
  105.  o    You can make a single, stand-alone .exe file from your program.
  106.  
  107.  o      Euphoria routines are naturally generic. The example
  108.     program below shows a single routine that will sort any
  109.     type of data -- integers, floating-point numbers, strings
  110.     etc. Euphoria is not an "Object-Oriented" language in the 
  111.     usual sense, yet it achieves many of the benefits of these
  112.     languages in a much simpler way. 
  113.  
  114.  
  115.  1.1 Example Program 
  116.  -------------------
  117.  
  118.  The following is an example of a complete Euphoria program.
  119.  
  120.  
  121. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  122.  
  123.  sequence list, sorted_list
  124.  
  125.  function merge_sort(sequence x)
  126.  -- put x into ascending order using a recursive merge sort
  127.      integer n, mid
  128.      sequence merged, a, b
  129.  
  130.      n = length(x)
  131.      if n = 0 or n = 1 then
  132.      return x  -- trivial case
  133.      end if
  134.  
  135.      mid = floor(n/2)
  136.      a = merge_sort(x[1..mid])       -- sort first half of x 
  137.      b = merge_sort(x[mid+1..n])     -- sort second half of x
  138.  
  139.      -- merge the two sorted halves into one
  140.      merged = {}
  141.      while length(a) > 0 and length(b) > 0 do
  142.      if compare(a[1], b[1]) < 0 then
  143.          merged = append(merged, a[1])
  144.          a = a[2..length(a)]
  145.      else
  146.          merged = append(merged, b[1])
  147.          b = b[2..length(b)]
  148.      end if
  149.      end while
  150.      return merged & a & b  -- merged data plus leftovers
  151.  end function
  152.  
  153.  procedure print_sorted_list()
  154.  -- generate sorted_list from list 
  155.      list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2}
  156.      sorted_list = merge_sort(list)
  157.      ? sorted_list   
  158.  end procedure
  159.  
  160.  print_sorted_list()     -- this command starts the program 
  161.  
  162.  
  163. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  164.  
  165.  
  166.  The above example contains 4 separate commands that are processed in order.
  167.  The first declares two variables: list and sorted_list to be sequences
  168.  (flexible arrays). The second defines a function merge_sort(). The third 
  169.  defines a procedure print_sorted_list(). The final command calls procedure 
  170.  print_sorted_list().
  171.  
  172.  The output from the program will be:
  173.   {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. 
  174.  
  175.  merge_sort() will just as easily sort {1.5, -9, 1e6, 100} or
  176.  {"oranges", "apples", "bananas"}.
  177.  
  178.  This example is stored as euphoria\demo\example.ex. This is not the fastest 
  179.  way to sort in Euphoria. Go to the euphoria\demo directory and type 
  180.  "ex allsorts" to see timings on several different sorting algorithms for 
  181.  increasing numbers of objects. For a quick tutorial example of Euphoria 
  182.  programming see euphoria\demo\bench\filesort.ex.
  183.  
  184.  
  185.  1.2 Installation 
  186.  ----------------
  187.  
  188.  To install Euphoria on your machine, first read the file install.doc. 
  189.  Installation simply involves copying the euphoria files to your hard disk 
  190.  under a directory named "EUPHORIA", and then modifying your autoexec.bat file
  191.  so that EUPHORIA\BIN is on your search path, and the environment variable 
  192.  EUDIR is set to the EUPHORIA directory. An automatic install program, 
  193.  "install.bat" is provided for this purpose. For the latest details, please 
  194.  read the instructions in install.doc before you run install.bat.
  195.  
  196.  When installed, the euphoria directory will look something like this: 
  197.  
  198.     \euphoria 
  199.         readme.doc
  200.         \bin        
  201.             ex.exe, search.ex, ed.bat, guru.bat, other utilities
  202.         \include
  203.             standard include files, e.g. graphics.e
  204.         \doc
  205.             documentation files, ed.doc, refman.doc etc.
  206.         \demo   
  207.             demo programs, e.g. ttt.ex, mset.ex, plot3d.ex
  208.  
  209.                 \langwar
  210.                     language war game, lw.ex
  211.                 \bench  
  212.                     benchmark programs
  213.            
  214.  
  215.  1.3 Running a Program
  216.  ------------------------
  217.  
  218.  Euphoria programs are executed by typing "ex", followed by the name of the 
  219.  main (or only) file. By convention, main Euphoria files have an extension of 
  220.  ".ex".  Other Euphoria files, that are meant to be included in a larger
  221.  program, end in ".e". Under Windows 95 Euphoria files can have long filenames.
  222.  To save typing, you can leave off the ".ex", and the ex command will supply it
  223.  for you automatically. If the file can't be found in the current directory, 
  224.  your PATH will be searched. There are no command-line options for ex itself, 
  225.  but your program can call the built-in function command_line() to read the 
  226.  ex command-line. You can redirect standard input and standard output when you
  227.  run a Euphoria program, for example:
  228.  
  229.     ex filesort.ex < raw.txt > sorted.txt
  230.  
  231.        or simply,
  232.  
  233.     ex filesort < raw.txt > sorted.txt
  234.  
  235.  For frequently-used programs you might want to make a small .bat (batch) file 
  236.  containing something like:
  237.      
  238.       @echo off
  239.       ex myprog.ex %1 %2 %3
  240.  
  241.  where myprog.ex expects at most three command-line arguments. This will save 
  242.  you from typing "ex" all the time. 
  243.  
  244.  You can also run bind.bat to combine your Euphoria program with the 
  245.  interpreter, ex.exe, to make a stand-alone .exe file. This is discussed 
  246.  further in 1.5 below.
  247.  
  248.  ex.exe is in the euphoria\bin directory which must be on your search path. 
  249.  Some Euphoria programs expect the environment variable EUDIR to be set to
  250.  the main Euphoria directory.
  251.  
  252.  Running Under Windows
  253.  ---------------------
  254.  You can run Euphoria programs directly from the Windows environment, or from 
  255.  a DOS shell that you have opened from Windows. By "associating" .ex files
  256.  with ex.exe, you can simply double-click on a .ex file to run it. Under
  257.  Windows 95 you would define a new file type for .ex, by clicking on
  258.  My Computer / view / options / file types. It is possible to have several 
  259.  Euphoria programs active in different windows. You can resize these windows, 
  260.  move them around, change to a different font, run things in the background, 
  261.  copy and paste between windows etc. See your Windows manual for details. The
  262.  Euphoria editor is available. You might want to associate .e, .pro 
  263.  (profiler output) and other text files with ed.bat. Also, the 
  264.  File-menu / Run command will let you type in ex or ed command lines. 
  265.  
  266.  Use of a swap file
  267.  ------------------
  268.  If you run a Euphoria program under Windows (or in a DOS shell under
  269.  Windows) and the program runs out of physical memory, it will start using 
  270.  "virtual memory". Windows provides this virtual memory automatically by 
  271.  swapping out the least-recently-used code and data to a system swap file. 
  272.  To change the size of the Windows swap file, click on Control Panel / 386 
  273.  Enhanced / "virtual memory...". Under OS/2 you can adjust the 
  274.  "DPMI_MEMORY_LIMIT" by clicking the Virtual DOS machine icon / "DOS Settings"
  275.  to allocate more extended memory for your program. 
  276.  
  277.  Under pure DOS, outside of Windows, there is no system swap file so the DOS-
  278.  extender built in to Euphoria will create one for possible use by your
  279.  program. This file is created when your Euphoria program starts up under DOS,
  280.  and is deleted when your program terminates. It starts as a 0-byte file and 
  281.  grows only if actual swapping is needed. It is created in the directory on 
  282.  your hard disk pointed to by the TEMP or TMP environment variable. If neither
  283.  of these variables have been set, it is created in the directory containing 
  284.  either ex.exe or your "bound" Euphoria .exe file. You can force it to be 
  285.  created in a particular directory by setting the CAUSEWAY environment 
  286.  variable as follows:
  287.      
  288.      SET CAUSEWAY=SWAP:path
  289.  
  290.  where  path  is the full path to the directory. You can prevent the creation 
  291.  of a DOS swap file with:
  292.      
  293.      SET CAUSEWAY=NOVM
  294.  
  295.  When disk swapping activity occurs your program will run correctly but will 
  296.  slow down. A better approach might be to free up more extended memory by 
  297.  cutting back on SMARTDRV and other programs that reserve large amounts of 
  298.  extended memory for themselves.
  299.  
  300.  
  301.  1.4 Editing a Program 
  302.  ---------------------
  303.  
  304.  You can use any text editor to edit a Euphoria program. However, Euphoria 
  305.  comes with its own special editor that is written entirely in Euphoria. 
  306.  Type: ed  followed by the complete name of the file you wish to edit (the 
  307.  .ex extension is not assumed). You can use this editor to edit any kind of 
  308.  text file. When you edit a .e or .ex file some extra features, such as color
  309.  syntax highlighting and auto-completion of certain statements, are available
  310.  to make your job easier. 
  311.  
  312.  Whenever you run a Euphoria program and get an error message, during 
  313.  compilation or execution, you can simply type ed with no file name and you 
  314.  will be automatically positioned in the file containing the error, at 
  315.  the correct line and column, and with the error message displayed at the 
  316.  top of the screen. 
  317.  
  318.  Under Windows you can associate ed.bat with various kinds of text files 
  319.  that you want to edit.
  320.  
  321.  Most keys that you type are inserted into the file at the cursor position. 
  322.  Hit the Esc key once to get a menu bar of special commands. The arrow keys, 
  323.  and the Insert/Delete Home/End PageUp/PageDown keys are also active. See 
  324.  the file euphoria\doc\ed.doc for a complete description of the editing 
  325.  commands. Esc h (help) will let you view ed.doc from your editing session.
  326.  
  327.  If you need to understand or modify any detail of the editor's operation, 
  328.  you can edit the file ed.ex in euphoria\bin (be sure to make a backup 
  329.  copy so you don't lose your ability to edit).  If the name ed conflicts 
  330.  with some other command on your system, simply rename the file 
  331.  euphoria\bin\ed.bat to something else.  Because this editor is written 
  332.  in Euphoria, it is remarkably concise and easy to understand. The same 
  333.  functionality implemented in a language like C, would take far more 
  334.  lines of code.
  335.  
  336.  
  337.  1.5 Distributing a Program
  338.  --------------------------
  339.  
  340.  Euphoria provides you with 3 distinct ways of distributing a program.
  341.  
  342.  In the first method you simply ship your users the Public Domain ex.exe 
  343.  file, along with your main Euphoria .ex file and any .e include files that
  344.  are needed (including any of the standard ones from euphoria\include). If 
  345.  the .ex and .e files are placed together in one directory and ex.exe is placed 
  346.  in the same directory or somewhere on the search PATH, then your user can
  347.  run your program by typing "ex" followed by the path of your .ex file. You 
  348.  might also provide a small .bat file so people won't actually have to type 
  349.  "ex". This method assumes that you are willing to share your Euphoria source
  350.  code with your users.
  351.  
  352.  If you want to distribute your program without exposing your Euphoria source
  353.  code then you can "shroud" or hide your source code by running shroud.bat.
  354.  shroud.bat will prompt you for the name of your main .ex file and the name of
  355.  the new shrouded .ex file that you want to create. Shrouding does the 
  356.  following to your program:
  357.  
  358.      1. Your .ex file is combined with all the .e files that it directly
  359.         or indirectly includes.
  360.      
  361.      2. All comments and blank lines are removed.
  362.      
  363.      3. All variable names and subroutine names are converted to short
  364.         meaningless names.
  365.      
  366.      4. All keywords are replaced by single-byte codes.
  367.      
  368.      5. (optionally) All strings are converted to sequences of ASCII codes
  369.         so they can't be easily read. 
  370.         
  371.  Step 5 goes beyond the typical .exe file produced by other languages, where 
  372.  you can easily read the character strings in the .exe. Shrouding not only
  373.  conceals your source code, it also combines it into a single, very compact 
  374.  file.
  375.  
  376.  Finally, there is a third method of distribution. You can convert your 
  377.  program into a single, stand-alone .exe file by running bind.bat. bind.bat 
  378.  will shroud your program as above, and then it will combine the shrouded form
  379.  of your program with the Public Domain ex.exe file to make a new .exe file 
  380.  that you can run. For example, if your program is called "myprog.ex" you can 
  381.  use bind to create "myprog.exe". Just type "bind" and answer the questions it 
  382.  asks. When bind is finished you can type:
  383.      
  384.      myprog
  385.  
  386.  to run your new myprog.exe file. This is equivalent to typing:
  387.      
  388.      ex myprog
  389.  
  390.  to run ex against your original myprog.ex file. Be sure to save myprog.ex
  391.  in a safe place.
  392.  
  393.  If you have just one program to distribute, and you want to conceal the 
  394.  source, you should make it into a single .exe file. If you have several 
  395.  small programs, you might want to shroud each of them and ship just one copy
  396.  of ex.exe to run them. This will save disk space and/or give you a smaller 
  397.  .ZIP file if you are going to make an archive out of them.
  398.  
  399.  Some further notes about shrouding and binding
  400.  ----------------------------------------------
  401.  
  402.  Your program should at least be free of syntax errors before you bind or
  403.  shroud it. If you are actively developing a program you'll find it is more
  404.  convenient to run it in the usual way with ex, rather than binding it and 
  405.  running it as a .exe file. Error messages generated against shrouded code 
  406.  will be difficult to relate back to your original source, since the line 
  407.  numbering and symbol names will be very different.
  408.  
  409.  Symbols declared as global in your main file are not renamed. This lets you
  410.  create a shrouded .e file containing a library of routines (and variables), 
  411.  all with meaningful names that other programmers can use by including your 
  412.  file. Try to make these global names at least 2 or 3 characters long to avoid 
  413.  conflicting with any short names already assigned by the shrouder. Any 
  414.  conflict will be detected by bind or shroud and you will be asked to pick 
  415.  a different (longer) name for the offending global symbol.
  416.  
  417.  shroud and bind can also be run non-interactively using command-line 
  418.  arguments. For example, you can just type:
  419.      
  420.      bind myprog 
  421.  or     bind -hide_strings myprog
  422.      
  423.  See the comments at the top of bin\bind.ex for details.
  424.  
  425.  Only the Public Domain Edition ex.exe file can be bound. Users of the
  426.  Euphoria Complete Edition will have files ex.exe (Complete) and pdex.exe 
  427.  (Public Domain) in euphoria\bin. The bind program will use the pdex.exe file
  428.  for binding. Debugging of large programs should be done by running ex.exe 
  429.  against the unshrouded .ex file.
  430.  
  431.  A one-line program will result in a 157K .exe file, but the size increases
  432.  extremely slowly as you add to your program. For instance, you can bind the 
  433.  entire Euphoria editor into a .exe of just 175K (178K if you hide the 
  434.  strings).
  435.  
  436.  The first two arguments returned by the command_line() library routine will 
  437.  be slightly different when your program is in .exe form. See library.doc 
  438.  for the details.
  439.  
  440.  A .exe file can handle standard input and output redirection. e.g.
  441.      
  442.      myprog.exe < file.in > file.out
  443.  
  444.  If you were to write a small .bat file "myprog.bat" that contained the line 
  445.  "ex myprog.ex" you would not be able to redirect input and output.
  446.      
  447.      myprog.bat < file.in > file.out         -- doesn't work!
  448.      
  449.  
  450.  Licensing
  451.  ---------
  452.  You have complete royalty-free rights to any Euphoria programs that you 
  453.  develop. You are free to distribute the Public Domain Edition ex.exe either
  454.  separately or bound with your program. You may incorporate any Euphoria .e or
  455.  .ex files from this package into your program, either "as is" or with your
  456.  modifications. (You will probably need at least a few of the standard include
  457.  files in any large program). We would appreciate it if you told people that 
  458.  your program was developed using Euphoria, but we do not require any such 
  459.  acknowledgment.
  460.  
  461.  The only file that you may *not* distribute is the ex.exe file that comes
  462.  with the Complete Edition.
  463.  
  464.             
  465.             2. Language Definition
  466.             ======================
  467.  
  468.  2.1 Objects
  469.  -----------
  470.  
  471.  All data objects in Euphoria are either atoms or sequences.  An atom is a 
  472.  single numeric value. A sequence is a collection of numeric values. 
  473.  The objects contained in a sequence can be an arbitrary mix of atoms or 
  474.  sequences. A sequence is represented by a list of objects in brace brackets,
  475.  separated by commas. Atoms can have any integer or double-precision floating 
  476.  point value. They can range from approximately -1e300 (minus one times 10 to 
  477.  the power 300) to +1e300 with 15 decimal digits of accuracy. Here are some
  478.  Euphoria objects:
  479.  
  480.     -- examples of atoms:
  481.     0
  482.     1000
  483.     98.6
  484.     -1e6
  485.  
  486.     -- examples of sequences:
  487.     {2, 3, 5, 7, 11, 13, 17, 19}        -- 8-element sequence
  488.     {1, 2, {3, 3, 3}, 4, {5, {6}}}      -- 5-element sequence
  489.     {{"jon", "smith"}, 52389, 97.25}    -- 3-element sequence
  490.     {}                                  -- 0-element sequence
  491.  
  492.  Numbers can also be entered in hexadecimal. For example:
  493.     
  494.     #FE             -- 254
  495.     #A000           -- 40960
  496.     #FFFF00008      -- 68718428168
  497.     -#10            -- -16
  498.  
  499.  Only the capital letters A, B, C, D, E, F are allowed in hex numbers.
  500.  
  501.  Sequences can be nested to any depth, i.e. you can have sequences within
  502.  sequences within sequences and so on to any depth (until you run out of 
  503.  memory). Brace brackets are used to construct sequences out of a list of 
  504.  expressions.  These expressions can be constant or evaluated at run-time. 
  505.  e.g.
  506.  
  507.     {x+6, 9, y*w+2, sin(0.5)}
  508.  
  509.  The "Hierarchical Objects" part of the Euphoria acronym comes from the
  510.  hierarchical nature of nested sequences. This should not be confused with
  511.  the class hierarchies of certain object-oriented languages. 
  512.  
  513.  Why do we call them "atoms"? Why not just "numbers"? Well, an atom *is* just a 
  514.  number, but we wanted to have a distinctive term that emphasizes that they are 
  515.  indivisible. Of course in the world of physics, atoms were split into smaller
  516.  parts many years ago, but in Euphoria you can't split them. They are the basic
  517.  building blocks of all the data that a Euphoria program can manipulate. With 
  518.  this analogy, sequences might be thought of as "molecules", made from atoms 
  519.  and other molecules. An better analogy would be that sequences are like 
  520.  directories, and atoms are like files. Just as a directory on your computer 
  521.  can contain both files and other directories, a sequence can contain both 
  522.  atoms and other sequences (and *those* sequences can contain atoms and 
  523.  sequences and so on). 
  524.  
  525.  Understanding atoms and sequences is the key to understanding Euphoria.
  526.  
  527.  Performance Note: Does this mean that all atoms are stored in memory as 
  528.  8-byte floating-point numbers? No. The Euphoria interpreter usually stores 
  529.  integer-valued atoms as machine integers (4 bytes) to save space and 
  530.  improve execution speed. When fractional results occur or numbers get too 
  531.  big, conversion to floating-point happens automatically. 
  532.  
  533.  
  534.  Character Strings
  535.  -----------------
  536.  Character strings may be entered using quotes e.g.
  537.  
  538.     "ABCDEFG"
  539.  
  540.  Strings are just sequences of characters, and may be manipulated and 
  541.  operated upon just like any other sequences. For example the above 
  542.  string is entirely equivalent to the sequence:
  543.  
  544.     {65, 66, 67, 68, 69, 70, 71}
  545.  
  546.  which contains the corresponding ASCII codes. Similarly, "" is
  547.  equivalent to {}. Both represent the sequence of length-0. As a 
  548.  matter of programming style, it is natural to use "" to suggest 
  549.  a length-0 sequence of characters, and {} to suggest some other 
  550.  kind of sequence.
  551.  
  552.  Individual characters may be entered using single quotes if it is 
  553.  desired that they be treated as individual numbers (atoms) and not
  554.  length-1 sequences. e.g.
  555.  
  556.      'B'   -- equivalent to the atom 66
  557.      "B"   -- equivalent to the sequence {66}
  558.  
  559.  Note that an atom is *not* equivalent to a one-element sequence containing 
  560.  the same value, although there are a few built-in routines that choose 
  561.  to treat them similarly.
  562.  
  563.  Special characters may be entered using a back-slash:
  564.  
  565.     \n        newline
  566.     \r        carriage return
  567.     \t        tab
  568.     \\        backslash
  569.     \"        double quote
  570.     \'        single quote
  571.  
  572.  For example, "Hello, World!\n", or '\\'. The Euphoria editor displays 
  573.  character strings in green. 
  574.  
  575.  Comments
  576.  --------
  577.  Comments are started by two dashes and extend to the end of the current line.
  578.  e.g.
  579.  
  580.      -- this is a comment
  581.  
  582.  Comments are ignored by the compiler and have no effect on execution speed. 
  583.  The editor displays comments in red. In this manual we use italics.
  584.  
  585.  
  586.  2.2 Expressions
  587.  ---------------
  588.  Like other programming languages, Euphoria lets you calculate results by
  589.  forming expressions. However, in Euphoria you can perform calculations on 
  590.  entire sequences of data with one expression, where in most other languages 
  591.  you would have to construct a loop. In Euphoria you can handle a sequence 
  592.  much as you would a single number. It can be copied, passed to a subroutine,
  593.  or calculated upon as a unit. For example,
  594.  
  595.     {1,2,3} + 5
  596.  
  597.  is an expression that adds the sequence {1,2,3} and the atom 5 to get the 
  598.  resulting sequence {6,7,8}. 
  599.  
  600.  
  601.  Subscripting of Sequences
  602.  -------------------------
  603.  A single element of a sequence may be selected by giving the element number 
  604.  in square brackets. Element numbers start at 1. Non-integer subscripts are 
  605.  rounded down to an integer. 
  606.  
  607.  For example, if x contains {5, 7, 9, 11, 13} then x[2] is 7. Suppose we 
  608.  assign something different to x[2]:
  609.  
  610.     x[2] = {11,22,33}
  611.  
  612.  Then x becomes: {5, {11,22,33}, 9, 11, 13}. Now if we ask for x[2] we get 
  613.  {11,22,33} and if we ask for x[2][3] we get the atom 33. If you try to 
  614.  subscript with a number that is outside of the range 1 to the number of 
  615.  elements, you will get a subscript error. For example x[0],  x[-99] or 
  616.  x[6] will cause errors. So will x[1][3] since x[1] is not a sequence. There 
  617.  is no limit to the number of subscripts that may follow a variable, but 
  618.  the variable must contain sequences that are nested deeply enough. The 
  619.  two dimensional array, common in other languages, can be easily simulated 
  620.  with a sequence of sequences:
  621.  
  622.      x = { {5, 6, 7, 8, 9},      -- x[1]
  623.        {1, 2, 3, 4, 5},      -- x[2]
  624.        {0, 1, 0, 1, 0} }     -- x[3]
  625.  
  626.  Where we have written out the numbers across three lines to make things 
  627.  clearer. An expression of the form x[i][j] can be used to access any element. 
  628.  The two dimensions are not symmetric however, since an entire "row" can be 
  629.  selected with x[i], but there is no simple expression to select an entire 
  630.  column. Other logical structures, such as n-dimensional arrays, arrays of 
  631.  strings, structures, arrays of structures etc. can also be handled easily and
  632.  flexibly:
  633.     
  634.  3-D array:
  635.      y = { {{1,1}, {3,3}, {5,5}},
  636.             {{0,0}, {0,1}, {9,1}},
  637.             {{-1,9},{1,1}, {2,2}} }
  638.      
  639.      y[2][3][1] is 9
  640.      
  641.  
  642.  Array of strings:
  643.  
  644.      s = {"Hello", "World", "Euphoria", "", "Last One"}
  645.      
  646.      s[3] is "Euphoria"
  647.      s[3][1] is 'E'
  648.  
  649.  A Structure:
  650.      
  651.      t = {
  652.            {"John","Smith"},
  653.           45000,
  654.           27,
  655.           185.5
  656.          } 
  657.  
  658.      To access "fields" or elements within a structure it is good programming
  659.      style to make up a set of constants that name the various fields. This
  660.      will make your program easier to read. For the example above you might 
  661.      have:
  662.      
  663.      constant NAME = 1
  664.      constant FIRST_NAME = 1, LAST_NAME = 2
  665.      
  666.      constant SALARY = 2
  667.      constant AGE = 3
  668.      constant WEIGHT = 4
  669.      
  670.      You could then access the person's name with t[NAME], or if you
  671.      wanted the last name you could say t[NAME][LAST_NAME].
  672.      
  673.  Array of structures:
  674.  
  675.      a = { 
  676.             {{"John","Smith"}, 45000, 27, 185.5},    -- a[1]
  677.            {{"Bill","Jones"}, 57000, 48, 177.2},    -- a[2]
  678.            
  679.            .... etc.
  680.          }
  681.  
  682.      a[2][SALARY] would be 57000.
  683.      
  684.  As we will see, Euphoria data structures are almost infinitely flexible.
  685.  You can easily add a new structure to the array of structures, or store an 
  686.  unusually long name in the NAME field and Euphoria will take care of it for 
  687.  you. Other languages would force you to declare a maximum size for everything,
  688.  and would make it difficult or impossible for you to grow beyond that size.
  689.  
  690.  Not only can a Euphoria program easily simulate all conventional data 
  691.  structures but you can create very useful, flexible structures that would be
  692.  extremely hard to declare in a conventional language.
  693.  
  694.  Note that expressions in general may not be subscripted, just variables. For 
  695.  example: {5+2,6-1,7*8,8+1}[3] is *not* supported. There are a few instances 
  696.  where this would be convenient, but in general it indicates that you are 
  697.  wastefully discarding some results that you have already calculated.
  698.  
  699.  
  700.  Slicing of Sequences
  701.  --------------------
  702.  A sequence of consecutive elements may be selected by giving the starting and 
  703.  ending element numbers. For example if x is {1, 1, 2, 2, 2, 1, 1, 1} then 
  704.  x[3..5] is the sequence {2, 2, 2}. x[3..3] is the sequence {2}. x[3..2] is 
  705.  also allowed. It evaluates to the length-0 sequence {}.  If y has the value: 
  706.  {"fred", "george", "mary"} then y[1..2] is {"fred", "george"}.
  707.  
  708.  We can also use slices for overwriting portions of variables. After x[3..5] = 
  709.  {9, 9, 9} x would be {1, 1, 9, 9, 9, 1, 1, 1}. We could also have said 
  710.  x[3..5] = 9 with the same effect. Suppose y is {0, "Euphoria", 1, 1}. 
  711.  Then y[2][1..4] is "Euph". If we say y[2][1..4]="ABCD" then y will 
  712.  become {0, "ABCDoria", 1, 1}.
  713.  
  714.  We need to be a bit more precise in defining the rules for empty slices. 
  715.  Consider a slice s[i..j] where s is of length n. A slice from i to j, 
  716.  where  j = i-1  and i >= 1 produces the empty sequence, even if i = n+1. 
  717.  Thus 1..0  and n+1..n and everything in between are legal (empty) slices. 
  718.  Empty slices are quite useful in many algorithms. A slice from i to j where 
  719.  j < i - 1 is illegal , i.e. "reverse" slices such as s[5..3] are not allowed. 
  720.  
  721.  
  722.  Only variables may be sliced, not expressions.
  723.  
  724.  
  725.  Relational Operators
  726.  --------------------
  727.  The relational operators  <  >  <=  >=  =  !=  each produce a 1 (true) or a 
  728.  0 (false) result. 
  729.  
  730.     1 > 0       -- 1 (true)
  731.     1 = 1       -- 1 (true)
  732.     4.4 >= 4.5  -- 0 (false)
  733.  
  734.  As we will soon see you can also apply these operators to sequences.
  735.  
  736.  
  737.  Logical Operators
  738.  -----------------
  739.  The logical operators 'and', 'or', and 'not' are used to determine the 
  740.  "truth" of an expression. e.g.
  741.  
  742.  
  743.     1 and 1     -- 1 (true)
  744.     1 and 0     -- 0 (false)
  745.     1 or  0     -- 1 (true)
  746.     0 or  0     -- 0 (false)
  747.     not 1       -- 0 (false)
  748.     not 0       -- 1 (true)
  749.     
  750.  You can also apply these operators to numbers other than 1 or 0. The rule is:
  751.  zero means false and non-zero means true. So for instance:
  752.  
  753.      5 and -4    -- 1 (true)
  754.      not 6       -- 0 (false)
  755.      
  756.  These operators can also be applied to sequences. See below.
  757.  
  758.  
  759.  Arithmetic Operators
  760.  --------------------
  761.  The usual arithmetic operators are available: add, subtract, multiply,
  762.  divide, unary minus, unary plus.
  763.  
  764.      3.5 + 3  -- 6.5
  765.      3 - 5    -- -2
  766.      6 * 2    -- 12
  767.      7 / 2    -- 3.5
  768.      -8.1     -- -8.1
  769.      +8       -- +8
  770.      
  771.  Computing a result that is too big (i.e. outside of -1e300 to +1e300) will 
  772.  result in one of the special atoms +infinity or -infinity. These appear as 
  773.  "inf" or "-inf" when you print them out. It is also possible to generate 
  774.  "nan" or "-nan". "nan" means "not a number", i.e. an undefined value (such as
  775.  inf / inf). These values are defined in the IEEE floating-point standard. If 
  776.  you see one of these special values in your output, it usually indicates an 
  777.  error in your program logic, although generating inf as an intermediate 
  778.  result may be acceptable in some cases. For instance, 1/inf is 0, which may 
  779.  be the "right" answer for your algorithm. 
  780.  
  781.  Division by zero, as well as bad arguments to math library routines, e.g. 
  782.  square root of a negative number, log of a non-positive number etc. cause an
  783.  immediate error message and your program is aborted.
  784.  
  785.  The only reason that you might use unary plus is to emphasize to the reader
  786.  of your program that a number is positive. The interpreter does not actually
  787.  calculate anything for this.
  788.  
  789.  Operations on Sequences
  790.  -----------------------
  791.  All of the relational, logical and arithmetic operators, as well as the math 
  792.  routines described in library.doc, can be applied to sequences as well as
  793.  to single numbers (atoms).
  794.  
  795.  When applied to a sequence, a unary (one operand) operator is actually 
  796.  applied to each element in the sequence to yield a sequence of results of the
  797.  same length. If one of these elements is itself a sequence then the same rule
  798.  is applied again recursively. e.g.
  799.  
  800.     x = -{1, 2, 3, {4, 5}}   -- x is {-1, -2, -3, {-4, -5}}
  801.  
  802.  If a binary (two-operand) operator has operands which are both sequences then 
  803.  the two sequences must be of the same length. The binary operation is then 
  804.  applied to corresponding elements taken from the two sequences to get a 
  805.  sequence of results. e.g.
  806.  
  807.     x = {5, 6, 7 {1, 1}} + {10, 10, 20, 100}
  808.     -- x is {15, 16, 27, {101, 101}}
  809.  
  810.  If a binary operator has one operand which is a sequence while the other is a 
  811.  single number (atom) then the single number is effectively repeated to 
  812.  form a sequence of equal length to the sequence operand. The rules for 
  813.  operating on two sequences then apply. Some examples:
  814.  
  815.     y = {4, 5, 6}
  816.  
  817.     w = 5 * y   -- w is {20, 25, 30}
  818.  
  819.     x = {1, 2, 3}
  820.     
  821.     z = x + y   -- z is {5, 7, 9}
  822.  
  823.     z = x < y   -- z is {1, 1, 1}
  824.  
  825.     w = {{1, 2}, {3, 4}, {5}}
  826.    
  827.     w = w * y   -- w is {{4, 8}, {15, 20}, {30}}
  828.  
  829.     w = {1, 0, 0, 1} and {1, 1, 1, 0}   -- w is {1, 0, 0, 0}
  830.     
  831.     w = not {1, 5, -2, 0, 0}            -- w is {0, 0, 0, 1, 1}
  832.  
  833.  
  834.  Concatenation of Sequences and Atoms - The '&' Operator
  835.  -------------------------------------------------------
  836.  Any two objects may be concatenated using the & operator. The result is a 
  837.  sequence with a length equal to the sum of the lengths of the concatenated 
  838.  objects (where atoms are considered here to have length 1). e.g.
  839.  
  840.     {1, 2, 3} & 4   -- result is {1, 2, 3, 4}
  841.  
  842.     4 & 5           -- result is {4, 5}
  843.  
  844.     {{1, 1}, 2, 3} & {4, 5}   -- result is {{1, 1}, 2, 3, 4, 5}
  845.  
  846.     x = {}  
  847.     y = {1, 2}
  848.     y = y & x       -- y is still {1, 2}
  849.  
  850.  
  851.  
  852.  Precedence Chart
  853.  ----------------
  854.  The precedence of operators is as follows:
  855.  
  856.     highest precedence:     function/type calls
  857.  
  858.                 unary-  unary+  not 
  859.  
  860.                 *  /
  861.  
  862.                 +  -
  863.  
  864.                 &
  865.  
  866.                 <  >  <=  >=  =  !=
  867.  
  868.      lowest precedence:     and, or
  869.  
  870.  Thus 2+6*3 means 2+(6*3) rather than (2+6)*3. Operators on the same line 
  871.  above have equal precedence and are evaluated left to right.
  872.  
  873.  Finally, sequence-formation, using braces and commas: 
  874.  
  875.      {a, b, c, ... } 
  876.  
  877.  is also an operator. It takes n operands, where n is 0 or more, and makes an
  878.  n-element sequence from their values. It might be listed at the bottom of the 
  879.  precedence chart, but there is never any ambiguity about the use of this 
  880.  operation in an expression. e.g.
  881.  
  882.      x = {apple, orange*2, {1,2,3}, 99/4+foobar}
  883.  
  884.  
  885.  2.3 Euphoria versus Conventional Languages
  886.  ------------------------------------------
  887.  By basing Euphoria on this one, simple, general, recursive data structure, 
  888.  a tremendous amount of the complexity normally found in programming languages
  889.  has been avoided. The arrays, structures, unions, arrays of records, 
  890.  multidimensional arrays, etc. of other languages can all be easily 
  891.  simulated in Euphoria with sequences. So can higher-level structures such
  892.  as lists, stacks, queues, trees etc.  
  893.  
  894.  Furthermore, in Euphoria you can have sequences of mixed type; you can 
  895.  assign any object to an element of a sequence; and sequences easily grow or 
  896.  shrink in length without your having to worry about storage allocation issues.
  897.  The exact layout of a data structure does not have to be declared in advance,
  898.  and can change dynamically as required. It is easy to write generic code,
  899.  where, for instance, you push or pop a mix of various kinds of data 
  900.  objects using a single stack. Making a flexible list that contains a variety 
  901.  of different kinds of data objects is trivial in Euphoria, but requires dozens
  902.  of lines of ugly code in other languages.
  903.  
  904.  Data structure manipulations are very efficient since the Euphoria interpreter
  905.  will point to large data objects rather than copy them. 
  906.  
  907.  Programming in Euphoria is based entirely on creating and manipulating 
  908.  flexible, dynamic sequences of data. Sequences are it - there are no
  909.  other data structures to learn. You operate in a simple, safe, elastic world 
  910.  of *values*, that is far removed from the rigid, tedious, dangerous world
  911.  of bits, bytes, pointers and machine crashes. 
  912.  
  913.  Unlike other languages such as LISP and Smalltalk, Euphoria's 
  914.  "garbage collection" of unused storage is a continuous process that never 
  915.  causes random delays in execution of a program, and does not pre-allocate 
  916.  huge regions of memory.   
  917.  
  918.  The language definitions of conventional languages such as C, C++, Ada, etc.
  919.  are very complex. Most programmers become fluent in only a subset of the 
  920.  language. The ANSI standards for these languages read like complex legal 
  921.  documents. 
  922.  
  923.  You are forced to write different code for different data types simply to 
  924.  copy the data, ask for its current length, concatenate it, compare it etc. 
  925.  The manuals for those languages are packed with routines such as "strcpy",
  926.  "strncpy", "memcpy", "strcat",  "strlen", "strcmp", "memcmp", etc. that 
  927.  each only work on one of the many types of data.
  928.  
  929.  Much of the complexity surrounds issues of data type. How do you define 
  930.  new types? Which types of data can be mixed? How do you convert one type 
  931.  into another in a way that will keep the compiler happy? When you need to 
  932.  do something requiring flexibility at run-time, you frequently find yourself 
  933.  trying to fake out the compiler.
  934.  
  935.  In these languages the numeric value 4 (for example) can have a different 
  936.  meaning depending on whether it is an int, a char, a short, a double, an  
  937.  int * etc. In Euphoria, 4 is the atom 4, period. Euphoria has something 
  938.  called types as we shall see later, but it is a much simpler concept.
  939.  
  940.  Issues of dynamic storage allocation and deallocation consume a great deal 
  941.  of programmer coding time and debugging time in these other languages, and 
  942.  make the resulting programs much harder to understand. Programs that must
  943.  run continuously often exhibit storage "leaks", since it takes a great
  944.  deal of discipline to safely and properly free all blocks of storage
  945.  once they are no longer needed.
  946.  
  947.  Pointer variables are extensively used. The pointer has been called the 
  948.  "go to" of data structures. It forces programmers to think of data as 
  949.  being bound to a fixed memory location where it can be manipulated in all 
  950.  sorts of low-level, non-portable, tricky ways. A picture of the actual 
  951.  hardware that your program will run on is never far from your mind. Euphoria
  952.  does not have pointers and does not need them.
  953.  
  954.  
  955.  2.4 Declarations
  956.  ----------------
  957.  
  958.  Identifiers
  959.  -----------
  960.  Variable names and other user-defined symbols (identifiers) may be of any 
  961.  length. Upper and lower case are distinct. Identifiers must start with a 
  962.  letter and then be followed by letters, digits or underscores. The 
  963.  following reserved words have special meaning in Euphoria and may not be 
  964.  used as identifiers:
  965.  
  966.  and            end             include          to
  967.  by             exit            not              type
  968.  constant       for             or               while
  969.  do             function        procedure        with
  970.  else           global          return           without
  971.  elsif          if              then 
  972.  
  973.  The Euphoria editor displays these words in blue.
  974.  
  975.  The following kinds of user-defined symbols may be declared in a program:
  976.  
  977.      o  procedures 
  978.     These perform some computation and may have a list of parameters, 
  979.     e.g.
  980.    
  981.     procedure empty()
  982.     end procedure
  983.  
  984.     procedure plot(integer x, integer y)
  985.         position(x, y)
  986.         puts(1, '*')
  987.     end procedure
  988.     
  989.     There are a fixed number of named parameters, but this is not 
  990.     restrictive since any parameter could be a variable-length sequence 
  991.     of arbitrary objects. In many languages variable-length parameter 
  992.     lists are impossible.  In C, you must set up strange mechanisms that
  993.     are complex enough that the average programmer cannot do it without
  994.     consulting a manual or a local guru. 
  995.  
  996.     A copy of the value of each argument is passed in. The formal 
  997.     parameter variables may be modified inside the procedure but this does
  998.     not affect the value of the arguments.
  999.  
  1000.     Performance Note: The interpreter does not actually copy sequences or
  1001.     floating-point numbers unless it becomes necessary. For example,
  1002.         
  1003.         y = {1,2,3,4,5,6,7,8.5,"ABC"}
  1004.         x = y
  1005.     
  1006.     The statement x = y does not actually cause a new copy of y to be 
  1007.     created. Both x and y will simply "point" to the same sequence. If we 
  1008.     later perform x[3] = 9, then a separate sequence will be created for x 
  1009.     in memory (although there will still be just one shared copy of 8.5 and 
  1010.     "ABC"). The same thing applies to "copies" of arguments passed in to
  1011.     subroutines.
  1012.     
  1013.      o  functions 
  1014.     These are just like procedures, but they return a value, and can be
  1015.     used in an expression, e.g.
  1016.  
  1017.     function max(atom a, atom b)
  1018.         if a >= b then
  1019.         return a
  1020.         else
  1021.         return b
  1022.         end if
  1023.     end function
  1024.  
  1025.     Any Euphoria object can be returned.  You can, in effect, have 
  1026.     multiple return values, by returning a sequence of objects. e.g.
  1027.     
  1028.     return {quotient, remainder}
  1029.  
  1030.     We will use the general term "subroutine", or simply "routine" when a
  1031.     remark is applicable to both procedures and functions.
  1032.  
  1033.      o  types 
  1034.     These are special functions that may be used in declaring the allowed
  1035.     values for a variable. A type must have exactly one parameter and 
  1036.     should return an atom that is either TRUE (non-zero) or FALSE (zero).
  1037.     Types can also be called just like other functions. They are discussed
  1038.     in more detail below.
  1039.  
  1040.      o  variables 
  1041.     These may be assigned values during execution e.g.
  1042.  
  1043.     integer x          -- x may only be assigned integer values
  1044.     x = 25
  1045.  
  1046.     object a, b, c     -- a, b and c may be assigned *any* value
  1047.     a = {}
  1048.     b = a
  1049.     c = 0
  1050.     
  1051.     When you declare a variable you name the variable (which protects you 
  1052.     against    making spelling mistakes later on) and you specify the values
  1053.     that may legally be assigned to the variable during execution of
  1054.     your program.
  1055.  
  1056.      o  constants
  1057.     These are variables that are assigned an initial value that can 
  1058.     never change e.g.
  1059.       
  1060.     constant MAX = 100
  1061.     constant Upper = MAX - 10, Lower = 5
  1062.     constant name_list = {"Fred", "George", "Larry"}
  1063.     
  1064.     The result of any expression can be assigned to a constant, even one
  1065.     involving calls to previously defined functions, but once the 
  1066.     assignment is made, the value of the constant variable is "locked in".
  1067.  
  1068.     Constants may not be declared inside a subroutine.
  1069.     
  1070.  Scope
  1071.  -----
  1072.  Every symbol must be declared before it is used. This is restrictive, but it
  1073.  has benefits. It means you always know in which direction to look for the 
  1074.  definition of a subroutine or variable that is used at some point in the 
  1075.  program. When looking at a subroutine definition, you know that there could 
  1076.  not be a call to this routine from any routine defined earlier. In general, 
  1077.  it makes you order your program such that there are low-level routines, 
  1078.  followed by higher-level routines. You can completely replace or rewrite a 
  1079.  routine without disturbing any routines or variables defined earlier. 
  1080.  You can read a .e or .ex source file from beginning to end without 
  1081.  encountering any variables or routines that haven't been defined yet.
  1082.  
  1083.  A symbol is defined from the point where it is declared to the end of its 
  1084.  "scope". The scope of a variable declared inside a procedure or function (a 
  1085.  private variable) ends at the end of the procedure or function.  The scope 
  1086.  of all other variables, constants, procedures, functions and types ends at 
  1087.  the end of the source file in which they are declared and they are referred 
  1088.  to as local, unless the word global precedes their declaration, in which case
  1089.  their scope extends indefinitely. Procedures and functions and types can call 
  1090.  themselves recursively. Mutual recursion, where routine A calls routine B 
  1091.  which in turn calls routine A, is not supported.
  1092.  
  1093.  Constant declarations must be outside of any subroutine. Constants cannot be
  1094.  private.
  1095.  
  1096.  Variable declarations inside a subroutine must all appear at the beginning,
  1097.  before the executable statements of the subroutine.
  1098.  
  1099.  A special case is that of the controlling variable used in a for-loop. It is 
  1100.  automatically declared at the beginning of the loop, and its scope ends at 
  1101.  the end of the for-loop. If the loop is inside a function or procedure, the 
  1102.  loop variable is a private variable and may not have the same name as any 
  1103.  other private variable. When the loop is at the top level, outside of any  
  1104.  function or procedure, the loop variable is a local variable and may not have 
  1105.  the same name as any other global or local variable in that file. You do not 
  1106.  declare loop variables as you would other variables. The range of values 
  1107.  specified in the for statement defines the legal values of the loop variable 
  1108.  - specifying a type would be redundant and is not allowed.
  1109.  
  1110.  
  1111.  Specifying the type of a variable
  1112.  ---------------------------------
  1113.  
  1114.  So far you've already seen some examples of variable types but now we will 
  1115.  define types more precisely.
  1116.  
  1117.  Variable declarations have a type name followed by a list of the variables 
  1118.  being declared. For example,
  1119.  
  1120.     object a 
  1121.  
  1122.     global integer x, y, z 
  1123.  
  1124.     procedure fred(sequence q, sequence r)
  1125.  
  1126.  The types: object, sequence, atom and integer are predefined. Variables of 
  1127.  type object may take on *any* value. Those declared with type sequence must  
  1128.  always be sequences. Those declared with type atom must always be atoms. Those
  1129.  declared with type integer must be atoms with integer values from -1073741824 
  1130.  to +1073741823 inclusive. You can perform exact calculations on larger integer
  1131.  values, up to about 15 decimal digits, but declare them as atom, rather than 
  1132.  integer.
  1133.  
  1134.  Note: In a procedure or function parameter list like the one for fred() above,
  1135.  a type name may only be followed by a single parameter name.
  1136.  
  1137.  Performance Note: Calculations using variables declared as integer will 
  1138.  usually be somewhat faster than calculations involving variables declared as
  1139.  atom. If your machine has floating-point hardware, Euphoria will use it to 
  1140.  manipulate atoms that aren't representable as integers. If your machine 
  1141.  doesn't have floating-point hardware, Euphoria will call software 
  1142.  floating-point arithmetic routines contained in ex.exe. You can force 
  1143.  Euphoria to bypass any floating-point hardware, by setting an environment 
  1144.  variable:
  1145.      
  1146.      SET NO87=1
  1147.  
  1148.  The slower software routines will be used, but this could be of some 
  1149.  advantage if you are worried about the floating-point bug in some early 
  1150.  Pentium chips.
  1151.  
  1152.  To augment the predefined types, you can create new types. All you have to 
  1153.  do is define a single-parameter function, but declare it with  
  1154.  type ... end type instead of function ... end function. For example,
  1155.  
  1156.  
  1157.     type hour(integer x)
  1158.          return x >= 0 and x <= 23
  1159.     end type
  1160.  
  1161.     hour h1, h2
  1162.  
  1163.     h1 = 10      -- ok
  1164.     h2 = 25      -- error! program aborts with a message
  1165.     
  1166.  Variables h1 and h2 can only be assigned integer values in the range 0 to 23
  1167.  inclusive. After each assignment to h1 or h2 the interpreter will call hour(), 
  1168.  passing the new value.  The value will first be checked to see if it is an 
  1169.  integer (because of "integer x"). If it is, the return statement will be 
  1170.  executed to test the value of x (i.e. the new value of h1 or h2).  If "hour"
  1171.  returns true, execution continues normally. If "hour" returns false then the
  1172.  program is aborted with a suitable diagnostic message. 
  1173.  
  1174.  "hour" can be used to declare subroutine parameters as well:
  1175.  
  1176.     procedure set_time(hour h)
  1177.  
  1178.  set_time() can only be called with a reasonable value for parameter h,
  1179.  otherwise the program will abort with a message.
  1180.  
  1181.  A variable's type will be checked after each assignment to the variable 
  1182.  (except where the compiler can predetermine that a check will not be 
  1183.  necessary), and the program will terminate immediately if the type function 
  1184.  returns false.  Subroutine parameter types are checked each time that the 
  1185.  subroutine is called. This checking guarantees that a variable can never have
  1186.  a value that does not belong to the type of that variable.
  1187.  
  1188.  Unlike other languages, the type of a variable does not affect any 
  1189.  calculations on the variable. Only the value of the variable matters in an 
  1190.  expression. The type just serves as an error check to prevent any "corruption"
  1191.  of the variable. 
  1192.  
  1193.  Type checking can be turned off or on between subroutines using the 
  1194.  "with type_check" or "without type_check" commands. It is initially on by
  1195.  default.
  1196.  
  1197.  Note to Benchmarkers: When comparing the speed of Euphoria programs against 
  1198.  programs written in other languages, you should specify  without type_check 
  1199.  at the top of the file, unless the other language provides a comparable 
  1200.  amount of run-time checking. This gives Euphoria permission to skip run-time 
  1201.  type checks, thereby saving some execution time. All other checks are still
  1202.  performed, e.g. subscript checking, uninitialized variable checking etc. 
  1203.  Even when you turn off type checking, Euphoria reserves the right to make 
  1204.  checks at strategic places, since this can actually allow it to run your 
  1205.  program faster in many cases. So you may still get a type check failure 
  1206.  even when you have turned off type checking. With or without type_check, 
  1207.  you will never get a machine-level exception. You will always get a 
  1208.  meaningful message from Euphoria when something goes wrong.
  1209.  
  1210.  Euphoria's method of defining types is much simpler than what you will find 
  1211.  in other languages, yet Euphoria provides the programmer with greater 
  1212.  flexibility in defining the legal values for a type of data. Any algorithm 
  1213.  can be used to include or exclude values. You can even declare a variable 
  1214.  to be of type object which will allow it to take on any value. Routines can 
  1215.  be written to work with very specific types, or very general types.
  1216.  
  1217.  Strict type definitions can greatly aid the process of debugging.  Logic 
  1218.  errors are caught close to their source and are not allowed to propagate in 
  1219.  subtle ways through the rest of the program. Furthermore, it is much easier 
  1220.  to reason about the misbehavior of a section of code when you are guaranteed 
  1221.  that the variables involved always had a legal value, if not the desired 
  1222.  value.
  1223.  
  1224.  Types also provide meaningful, machine-checkable documentation about your 
  1225.  program, making it easier for you or others to understand your code at a 
  1226.  later date. Combined with the subscript checking, uninitialized variable 
  1227.  checking, and other checking that is always present, strict run-time type 
  1228.  checking makes debugging much easier in Euphoria than in most other 
  1229.  languages. It also increases the reliability of the final program since 
  1230.  many latent bugs that would have survived the testing phase in other 
  1231.  languages will have been caught by Euphoria.
  1232.  
  1233.  Anecdote 1: In porting a large C program to Euphoria, a number
  1234.  of latent bugs were discovered. Although this C program was believed to be 
  1235.  totally "correct", we found: a situation where an uninitialized variable 
  1236.  was being read; a place where element number "-1" of an array was routinely 
  1237.  written and read; and a situation where something was written just off the 
  1238.  screen. These problems resulted in errors that weren't easily visible to a 
  1239.  casual observer, so they had survived testing of the C code. 
  1240.  
  1241.  Anecdote 2: The Quick Sort algorithm presented on page 117 of Writing 
  1242.  Efficient Programs by Jon Bentley has a subscript error! The algorithm will 
  1243.  sometimes read the element just before the beginning of the array to be 
  1244.  sorted, and will sometimes read the element just after the end of the array. 
  1245.  Whatever garbage is read, the algorithm will still work - this is probably 
  1246.  why the bug was never caught. But what if there isn't any (virtual) memory 
  1247.  just before or just after the array? Bentley later modifies the algorithm 
  1248.  such that this bug goes away -- but he presented this version as being 
  1249.  correct. Even the experts need subscript checking!
  1250.  
  1251.  Performance Note: When typical user-defined types are used extensively, type 
  1252.  checking adds only 20 to 40 percent to execution time. Leave it on unless 
  1253.  you really need the extra speed. You might also consider turning it off for
  1254.  just a few heavily-executed routines. Profiling can help with this decision.
  1255.  
  1256.  
  1257.  2.5 Statements
  1258.  --------------
  1259.  
  1260.  The following kinds of executable statements are available:
  1261.  
  1262.     o   assignment statement
  1263.  
  1264.     o   procedure call 
  1265.  
  1266.     o   if statement
  1267.  
  1268.     o   while statement
  1269.  
  1270.     o   for statement 
  1271.  
  1272.     o   return statement
  1273.  
  1274.     o   exit statement
  1275.  
  1276.  Semicolons are not used in Euphoria, but you are free to put as many 
  1277.  statements as you like on one line, or to split a single statement across 
  1278.  many lines. You may not split a statement in the middle of a variable name, 
  1279.  string, number or keyword. 
  1280.  
  1281.  An assignment statement assigns the value of an expression to a simple 
  1282.  variable, or to a subscript or slice of a variable. e.g. 
  1283.     
  1284.     x = a + b
  1285.  
  1286.     y[i] = y[i] + 1
  1287.  
  1288.     y[i..j] = {1, 2, 3}
  1289.  
  1290.  The previous value of the variable, or element(s) of the subscripted or 
  1291.  sliced variable are discarded.  For example, suppose x was a 1000-element 
  1292.  sequence that we had initialized with:
  1293.  
  1294.     object x
  1295.  
  1296.     x = repeat(0, 1000)  -- a sequence of 1000 zeros
  1297.  
  1298.  and then later we assigned an atom to x with:
  1299.  
  1300.     x = 7
  1301.  
  1302.  This is perfectly legal since x is declared as an object. The previous value 
  1303.  of x, namely the 1000-element sequence, would simply disappear. Actually, 
  1304.  the space consumed by the 1000-element sequence will be automatically 
  1305.  recycled due to Euphoria's dynamic storage allocation.
  1306.  
  1307.  Note that the equals symbol '=' is used for both assignment and for equality
  1308.  testing. There is never any confusion because an assignment statement in
  1309.  Euphoria is a statement only, not an expression (as in C).
  1310.  
  1311.  A procedure call starts execution of a procedure, passing it an optional list 
  1312.  of argument values. e.g.
  1313.  
  1314.     plot(x, 23)
  1315.  
  1316.  An if statement tests an expression to see if it is 0 (false) or non-zero 
  1317.  (true) and then executes the appropriate series of statements.  There may 
  1318.  be optional elsif and else clauses. e.g.
  1319.  
  1320.     if a < b then
  1321.         x = 1
  1322.     end if
  1323.  
  1324.  
  1325.     if a = 9 then
  1326.         x = 4
  1327.         y = 5
  1328.     else
  1329.         z = 8
  1330.     end if
  1331.  
  1332.  
  1333.     if char = 'a' then
  1334.         x = 1
  1335.     elsif char = 'b' then
  1336.         x = 2
  1337.     elsif char = 'c' then
  1338.         x = 3
  1339.     else
  1340.         x = -1
  1341.     end if
  1342.  
  1343.  Notice that "elsif" is a contraction of "else if", but it's cleaner because
  1344.  it doesn't require an "end if" to go with it. There is just one "end if" for 
  1345.  the entire if statement, even when there are many elsif's contained in it.
  1346.  
  1347.  A while statement tests an expression to see if it is non-zero (true), 
  1348.  and while it is true a loop is executed. e.g.
  1349.  
  1350.     while x > 0 do
  1351.         a = a * 2
  1352.         x = x - 1
  1353.     end while
  1354.  
  1355.  A for statement sets up a special loop with a controlling loop variable 
  1356.  that runs from an initial value up or down to some final value. e.g.
  1357.  
  1358.     for i = 1 to 10 do
  1359.         ? i   -- ? is a short form for print() -- see library.doc
  1360.     end for
  1361.  
  1362.     for i = 10 to 20 by 2 do
  1363.         for j = 20 to 10 by -2 do
  1364.         ? {i, j}
  1365.         end for
  1366.     end for
  1367.  
  1368.  The loop variable is declared automatically and exists until the end of the 
  1369.  loop. Outside of the loop the variable has no value and is not even declared.
  1370.  If you need its final value, copy it into another variable before leaving 
  1371.  the loop. The compiler will not allow any assignments to a loop variable. The
  1372.  initial value, loop limit and increment must all be atoms. If no increment 
  1373.  is specified then +1 is assumed. The limit and increment values are 
  1374.  established when the loop is entered, and are not affected by anything that 
  1375.  happens during the execution of the loop. 
  1376.  
  1377.  A return statement returns from a subroutine. If the subroutine is a function 
  1378.  or type then a value must also be returned. e.g.
  1379.  
  1380.     return
  1381.  
  1382.     return {50, "FRED", {}}
  1383.  
  1384.  An exit statement may appear inside a while-loop or a for-loop. It causes 
  1385.  immediate termination of the loop, with control passing to the first statement
  1386.  after the loop. e.g.
  1387.  
  1388.     for i = 1 to 100 do
  1389.         if a[i] = x then
  1390.         location = i
  1391.         exit
  1392.         end if
  1393.     end for
  1394.  
  1395.  It is also quite common to see something like this:
  1396.     
  1397.     constant TRUE = 1
  1398.     
  1399.     while TRUE do
  1400.          ...
  1401.          if some_condition then
  1402.             exit
  1403.          end if
  1404.          ...
  1405.     end while
  1406.  
  1407.  i.e. an "infinite" while-loop that actually terminates via an exit statement 
  1408.  at some arbitrary point in the body of the loop. 
  1409.  
  1410.  Note: if you happen to create a real infinite loop, with no input/output 
  1411.  taking place, there is no easy way to stop it. You will have to type 
  1412.  Control-Alt-Delete to either reboot, or (under Windows) terminate your 
  1413.  DOS prompt session. If the program had files open for writing, it would be 
  1414.  advisable to run chkdsk or scandisk to check your file system integrity.
  1415.  
  1416.  When a Euphoria program is waiting for keyboard input, you can press 
  1417.  control-C to abort the program.
  1418.  
  1419.  
  1420.  2.6 Top-Level Commands
  1421.  ----------------------
  1422.  
  1423.  Euphoria processes your .ex file in one pass, starting at the first line and 
  1424.  proceeding through to the last line. When a procedure or function definition 
  1425.  is encountered, the routine is checked for syntax and converted into an 
  1426.  internal form, but no execution takes place. When a statement that is outside 
  1427.  of any routine is encountered, it is checked for syntax, converted into an 
  1428.  internal form and then immediately executed.  If your .ex file contains only 
  1429.  routine definitions, but no immediate execution statements, then nothing will 
  1430.  happen when you try to run it (other than syntax checking). You need to have 
  1431.  an immediate statement to call your main routine (see the example program in 
  1432.  section 1.1). It is quite possible to have a .ex file with nothing but 
  1433.  immediate statements, for example you might want to use Euphoria as a 
  1434.  simple calculator, typing in just one print (or ?) statement into a file, and 
  1435.  then executing it. To give the user something to look at, the langwar demo 
  1436.  program reads in and displays a file on the screen, before the rest of the 
  1437.  program is compiled (on a 486 or higher this makes little difference as the 
  1438.  compiler takes less than a second to finish compiling the entire program). 
  1439.  Another common practice is to immediately initialize a global variable, just
  1440.  after its declaration.
  1441.  
  1442.  The following special commands may only appear at the top level i.e. 
  1443.  outside of any function or procedure. As we have seen, it is also 
  1444.  possible to use any Euphoria statement, including for-loops, while-loops, 
  1445.  if statements etc. (but not return), at the top level.
  1446.  
  1447.  include filename - reads in (compiles) a Euphoria source file in the presence 
  1448.            of any global symbols that have already been defined. 
  1449.            These global symbols are visible within the included file.
  1450.            Only those symbols defined as "global" in the included file
  1451.            are visible (accessible) in the remainder of the program. 
  1452.            If an absolute filename is given, Euphoria will use it. When
  1453.            a relative filename is given, Euphoria will first look for 
  1454.            it in the same directory as the main file given on the ex 
  1455.            command line. If it's not there, it will look in 
  1456.            %EUDIR%\include, where EUDIR is the environment variable 
  1457.            that must be set when using Euphoria. This directory 
  1458.            contains the standard Euphoria include files.
  1459.            
  1460.            An included file can include other files. In fact, you can
  1461.            "nest" included files up to 10 levels deep.
  1462.            
  1463.            An include statement will be quietly ignored if the file has
  1464.            already been included, directly or indirectly.
  1465.  
  1466.            An include statement must be written on a line by itself.
  1467.            Only a comment can appear after it on the same line.
  1468.            
  1469.  with            - turns on one of the compile options: profile, trace, 
  1470.            warning or type_check. Options warning and type_check are
  1471.            initially on, while profile and trace are initially off. 
  1472.  
  1473.  without         - turns off one of the above options. Note that each of 
  1474.            these options may be turned on or off between subroutines 
  1475.            but not inside of a subroutine. These options apply 
  1476.            globally. For example if you have:
  1477.  
  1478.             without type_check
  1479.             include graphics.e 
  1480.  
  1481.            then type checking will be turned off inside graphics.e as 
  1482.            well as in the current file. 
  1483.  
  1484.  
  1485.  Profiling         
  1486.  ---------
  1487.  If you specify "with profile" then an execution profile will be produced
  1488.  when your program finishes execution. It is written to the file "ex.pro" in
  1489.  the current directory.
  1490.  
  1491.  A profile is a listing of your program showing the number of times each 
  1492.  statement was executed. Only statements compiled after "with profile" will be 
  1493.  shown. Normally you will say "with profile" at the top of your main .ex file,
  1494.  so you can get a complete listing. View this file with the Euphoria editor to
  1495.  see a color display.
  1496.  
  1497.  Profiling can help you in many ways: it lets you see which statements are 
  1498.  heavily executed, so you can try to speed up your program; it lets you verify
  1499.  that your program is actually working the way you intended; and it lets you
  1500.  see which sections of code were not tested - don't let your users be the 
  1501.  first!
  1502.    
  1503.  
  1504.             3. Debugging
  1505.             ============
  1506.  
  1507.  
  1508.  Debugging in Euphoria is much easier than in most other programming languages.
  1509.  The extensive run-time checking provided at all times by Euphoria automatically
  1510.  catches many bugs that in other languages might take hours of your time to 
  1511.  track down. When Euphoria catches an error, you will always get a brief 
  1512.  report on your screen, and a detailed report in a file called "ex.err". 
  1513.  These reports always include a full English description of what happened, 
  1514.  along with a call-stack traceback. The file ex.err will also have a dump of 
  1515.  all variable values, and optionally a list of the most recently executed 
  1516.  statements. For extremely large sequences, only a partial dump is shown.
  1517.  
  1518.  In addition, you are able to create user-defined types that precisely 
  1519.  determine the set of legal values for each of your variables. An error report
  1520.  will occur the moment that one of your variables is assigned an illegal value.
  1521.  
  1522.  Sometimes a program will misbehave without failing any run-time checks. In 
  1523.  any programming language it may be a good idea to simply study the source 
  1524.  code and rethink the algorithm that you have coded. It may also be useful 
  1525.  to insert print statements at strategic locations in order to monitor the 
  1526.  internal logic of the program. This approach is particularly convenient in 
  1527.  an interpreted language like Euphoria since you can simply edit the source
  1528.  and rerun the program without waiting for a recompile/relink.  
  1529.  
  1530.  Euphoria provides you with additional powerful tools for debugging. You 
  1531.  can trace the execution of your program source code on one screen while 
  1532.  you witness the output of your program on another. with trace / without trace 
  1533.  commands select the subroutines in your program that are available for tracing.
  1534.  Often you will simply insert a "with trace" command at the very beginning of 
  1535.  your source code to make it all traceable. Sometimes it is better to place 
  1536.  the first "with trace" after all of your user-defined types, so you don't 
  1537.  trace into these routines after each assignment to a variable. At other times,
  1538.  you may know exactly which routine or routines you are interested in tracing, 
  1539.  and you will want to select only these ones. Of course, once you are in the 
  1540.  trace window you can interactively skip over the execution of any routine by 
  1541.  pressing down-arrow on the keyboard rather than Enter. 
  1542.  
  1543.  Only traceable lines can appear in ex.err as "most-recently-executed lines" 
  1544.  should a run-time error occur. If you want this information and didn't get it, 
  1545.  you should insert a "with trace" and then rerun your program. Execution will 
  1546.  be a bit slower when lines compiled "with trace" are executed.
  1547.  
  1548.  After you have predetermined the lines that are traceable, your program must 
  1549.  then dynamically cause the trace facility to be activated by executing a 
  1550.  trace(1) statement. Again, you could simply say:
  1551.  
  1552.     with trace
  1553.     trace(1)    -- or trace(2) if you prefer a mono display
  1554.  
  1555.  at the top of your program, so you can start tracing from the beginning of 
  1556.  execution. More commonly, you will want to trigger tracing when a certain 
  1557.  routine is entered, or when some condition arises. e.g.
  1558.     
  1559.     if x < 0 then
  1560.         trace(1)
  1561.     end if
  1562.  
  1563.  You can turn off tracing by executing a trace(0) statement. You can also 
  1564.  turn it off interactively by typing 'q' to quit tracing. Remember that 
  1565.  "with trace" must appear outside of any routine, whereas trace(1) and 
  1566.  trace(0) can appear inside a routine or outside.
  1567.  
  1568.  You might want to turn on tracing from within a type. Suppose you run 
  1569.  your program and it fails, with the ex.err file showing that one of your 
  1570.  variables has been set to a strange, although not illegal value, and you 
  1571.  wonder how it could have happened. Simply create a type for that variable 
  1572.  that executes trace(1) if the value being assigned to the variable is the 
  1573.  strange one that you are interested in.
  1574.  e.g.
  1575.     type positive_int(integer x)
  1576.         if x = 99 then
  1577.         trace(1) -- how can this be???
  1578.         return 1 -- keep going
  1579.         else
  1580.         return x > 0
  1581.         end if
  1582.     end type
  1583.  
  1584.  You will then be able to see the exact statement that caused your variable 
  1585.  to be set to the strange value, and you will be able to check the values 
  1586.  of other variables. You will also be able to check the output screen to 
  1587.  see what has been happening up to this precise moment. If you make your 
  1588.  special type return 0 for the strange value instead of 1, you can force a 
  1589.  dump into ex.err.
  1590.  
  1591.  The Trace Screen
  1592.  ----------------
  1593.  When a trace(1) statement is executed, your main output screen is saved and 
  1594.  a trace screen appears. It shows a view of your program with the statement 
  1595.  that will be executed next highlighted, and several statements before and 
  1596.  after showing as well. Several lines at the bottom of the screen are 
  1597.  reserved for displaying variable names and values. The top line shows the 
  1598.  commands that you can enter at this point:
  1599.  
  1600.  F1 - display main output screen - take a look at your program's output so far
  1601.  
  1602.  F2 - redisplay trace screen. Press this key while viewing the main output 
  1603.       screen to return to the trace display.
  1604.  
  1605.  Enter - execute the currently-highlighted statement only
  1606.  
  1607.  down-arrow - continue execution and break when any statement coming after 
  1608.           this one in the source listing is about to be executed. This 
  1609.           lets you skip over subroutine calls. It also lets you force your
  1610.           way out of repetitive loops.
  1611.  
  1612.  ? - display the value of a variable. Many variables are displayed 
  1613.      automatically as they are assigned a value, but sometimes you will have 
  1614.      to explicitly ask for one that is not on display. After hitting ?
  1615.      you will be prompted for the name of the variable. Variables that are 
  1616.      not defined at this point cannot be shown. Variables that have not yet 
  1617.      been initialized will have <NO VALUE> beside their name. Only variables,
  1618.      not general expressions, can be displayed.
  1619.  
  1620.  q - quit tracing and resume normal execution. Tracing will start again when 
  1621.      the next trace(1) is executed.
  1622.  
  1623.  ! - this will abort execution of your program. A traceback and dump of 
  1624.      variable values will go to ex.err.
  1625.  
  1626.  As you trace your program, variable names and values appear automatically in 
  1627.  the bottom portion of the screen. Whenever a variable is assigned-to, you will 
  1628.  see its name and new value appear at the bottom. This value is always kept 
  1629.  up-to-date. Private variables are automatically cleared from the screen 
  1630.  when their routine returns. When the variable display area is full, 
  1631.  least-recently referenced variables will be discarded to make room for 
  1632.  new variables. The value of a long sequence will be cut off after 80 
  1633.  characters.
  1634.  
  1635.  For your convenience, numbers that are in the range of printable ASCII 
  1636.  characters (32-127) are displayed along with the ASCII character itself. The
  1637.  ASCII character will be in a different color (or in quotes in a mono display).
  1638.  This is done for all variables, since Euphoria does not know in general
  1639.  whether you are thinking of a number as an ASCII character or not. You will
  1640.  also see ASCII characters (in quotes) in ex.err. This can make for a rather
  1641.  "busy" display, but the ASCII information is often very useful.
  1642.  
  1643.  The trace screen adopts the same graphics mode as the main output screen. 
  1644.  This makes flipping between them quicker and easier.
  1645.  
  1646.  When a traced program requests keyboard input, the main output screen will 
  1647.  appear, to let you type your input as you normally would. This works fine for 
  1648.  gets() (read one line) input. When get_key() (quickly sample the keyboard) is
  1649.  called you will be given 10 seconds to type a character, otherwise it is 
  1650.  assumed that there is no input for this call to get_key(). This allows you to
  1651.  test the case of input and also the case of no input for get_key().
  1652.  
  1653.              --- END OF PART I ---
  1654.  
  1655.          see library.doc for Part II - Library Routines
  1656.          
  1657.  
  1658.